package demo1;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-08-09
 * Time: 17:25
 */
/*class Tests {
    static boolean foo(char c) {
        System.out.print(c);
        return true;
    }
}*/

public class Test {
    public static int lengthOfLastWord(String s) {
        int len = s.length();
        int right = len-1;
        int count = 0;
        for (int i = right; i >= 0; i--) {
            char ch = s.charAt(i);
            if(ch == ' '){
               if(count == 0){
                   s.trim();
               }else {
                   return count;
               }
            }else{
                count++;
            }
        }
        return count;
    }
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int k = m + n - 1;
        int x = m - 1;
        int y = n - 1;
        while (x != 0 && y != 0){
            if(nums1[x] > nums2[y]){
                nums1[k] = nums1[x];
                k--;
                x--;
            }else {
                nums1[k] = nums2[y];
                k--;
                y--;
            }
        }
        while (x != 0){
            nums1[k] = nums1[x];
            k--;
            x--;
        }
        while (n != 0){
            nums1[k] = nums1[y];
            k--;
            y--;
        }
    }


    public static String reverseOnlyLetters(String s) {
        int left = 0;
        int right = s.length() - 1;
        char[] arr = s.toCharArray();
        while (true) {
            while (left < right && !Character.isLetter(arr[left])) {
                left++;
            }
            while (right > left && !Character.isLetter(arr[right])) {
                right--;
            }
            if (left >= right) {
                break;
            }
            char temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }
        return new String(arr);
    }

    public static void main(String[] args) {
        String str = "ab-cd";
        String ret = reverseOnlyLetters(str);
        System.out.println(ret);
    }
    public static boolean containsDuplicate(int[] nums) {
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] == nums[i+1]){
                return true;
            }
        }
        return false;
    }
    public static void main3(String[] args) {
        int[] arry = {1,2,3,4,1};
        boolean ret = containsDuplicate(arry);
        System.out.println(ret);
    }

    public static void main2(String[] args) {
        int[] nums1 = {1,3,5};
        int[] nums2 = {2,4,6};
        int len1 = nums1.length;
        int len2 = nums2.length;
        merge(nums1,len1,nums2,len2);

    }
    public static void main1(String[] args) {
        String str = "hello world";
        int ret = lengthOfLastWord(str);
        System.out.println(ret);
    }

}
